package com.haojin;

import org.junit.Test;

/**
 * @author .29.
 * @create 2022-04-03 20:59
 */
public class StringDemo2 {
    /*
    获取两个字符串最大的相同子串。比如：
     str1 = "abcwerthelloyuiodef";str 2 = "cvhellobnm"
     */
//前提：两个字串字符串中只有一个最大相同子串
    public String getMaxString(String str1, String str2) {
        if (str1 != null && str2 != null) {
            String maxStr = (str1.length() >= str2.length()) ? str1 : str2;
            String minStr = (str1.length() < str2.length()) ? str1 : str2;
            int length = minStr.length();
            for (int i = 0; i < length; i++) {
                for (int x = 0, y = length - i; y <= length; x++, y++) {
                    String subStr = minStr.substring(x, y);
                    if (maxStr.contains(subStr)) {
                        return subStr;
                    }
                }
            }
        }
        return null;
    }
@Test
    public void getMaxString(){
        String str1 = "abcwerthelloyuiodef";
        String  str2 = "cvhellobnm";
    System.out.println(getMaxString(str1,str2));
}
}
